package com.situ.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/cookie1")
public class Cookie1Servlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("Cookie1Servlet.service()");
		//http://localhost:8080/Java1912Web2/cookie1?goods=IPhone
		String goodsName = req.getParameter("goods");
		// 设置Cookie信息到浏览器客户端
		Cookie cookie1 = new Cookie("goods", goodsName);
		cookie1.setMaxAge(10 * 60);// 单位是秒Second，10分钟
		// cookie1.setMaxAge(24 * 60 * 60);// 代表24小时
		Cookie cookie2 = new Cookie("name", "zhangsan");
		cookie2.setMaxAge(-100);
		resp.addCookie(cookie1);
		resp.addCookie(cookie2);
		
		// 每个用户有自己唯一的一个Session
		HttpSession session = req.getSession();
		session.setAttribute("age", 23);
		// session.setMaxInactiveInterval();
		req.setAttribute("age", 34);
	}
	
}
